define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'setting/banner/index' + location.search,
                    add_url: 'setting/banner/add',
                    edit_url: 'setting/banner/edit',
                    del_url: 'setting/banner/del',
                    multi_url: 'setting/banner/multi',
                    import_url: 'setting/banner/import',
                    dragsort_url: '',
                    table: 'banner',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'category.name', title: __('Category_id'),operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'url_type', title: __('Url_type'), searchList: {"10":__('Url_type 10'),"20":__('Url_type 20'),"30":__('Url_type 30'),"40":__('Url_type 40'),"50":__('Url_type 50')}, formatter: Table.api.formatter.normal},
                        {field: 'parameter', title: __('Parameter'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                function fun(val) {
                    $('#c-url').parents('.form-group').hide();
                    $('#c-content').parents('.form-group').hide();
                    $('#c-url_type').parents('.form-group').hide();
                    $('input[id^="c-parameter"]').parents('.form-group').hide();
                    switch (val) {
                        case '1':
                            $('#c-url').parents('.form-group').show();
                            break;
                        case '2':
                            $('#c-content').parents('.form-group').show();
                            break;
                        case '3':
                            $('#c-url_type').parents('.form-group').show();
                            urlTypeFun($("#c-url_type").val());
                            break;
                    }
                }

                function urlTypeFun(val){
                    $('input[id^="c-parameter-"]').parents('.form-group').hide();
                    var obj = $('input[id^="c-parameter-"]');
                    $.each(obj, function(key, value) {
                        value.selectPageClear;
                        value.selectPageRefresh;
                    });
                    $('#c-parameter-'+val).parents('.form-group').show();
                }

                fun($("#c-type").val());

                $(document).on("change", "#c-type", function () {
                    fun($(this).val());
                });
                $(document).on("change", "#c-url_type", function () {
                    urlTypeFun($(this).val());
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
